import random;

class Activity(object):
    def __init__(self, stgm, sAct, aPersons, nTime, sId):
        self.stgm = stgm;
        self.nTime = nTime;
        self.sActivity = sAct;
        self.aPersons = aPersons;
        self.sId = sId;
        self.aChildNodes = [];
        self.nLikelihood = 0;
    
    def makeXmlGraph(self, graph):
        graph.addnode(self.rootAct, [], self.rootAct.aScores);
        for childNode in self.aChildNodes:
            aParents = [];
            for parentId in childNode.sParentsId:
                aParents.append(graph.idndict[parentId]);
            graph.addnode(childNode, aParents, childNode.aScores);
    
    def makeXmlNode(self, sId, sActName, aSubjects, aObjects, aTimes, sParentsId, aScores, sLocation, bRoot):        
        sName = self.getNodeName(sId, sActName, aTimes[len(aTimes)-1], aSubjects);
        node = self.stgm.Node(sName, nId=sId, actName=sActName, aSubjects=aSubjects, timeId=self.nTime, aTimes=aTimes, 
                              aScores=aScores, aObjects=aObjects, location=sLocation, sParentsId=sParentsId);
        if bRoot: 
            self.rootAct = node;
            self.nLikelihood = aScores[0];
        else: self.aChildNodes.append(node);
    
    def makeRndGraph(self, graph, nPrior):
        sTime = "t"+str(self.nTime);
        sName = self.getNodeName(self.sId, self.sActivity, sTime, self.aPersons);
        sObsId = self.sId+".1";
        obsName = sObsId+"-Obs_t"+str(self.nTime);
        self.nLikelihood = random.random();
        self.rootAct = self.stgm.Node(sName, nId=self.sId, actName=self.sActivity, 
                                      aSubjects=self.aPersons, aTimes=[sTime], 
                                      aScores=[self.nLikelihood], aObjects=[], location="");
        self.obsNode = self.stgm.Node(obsName, nId=sObsId, actName="Observation", 
                                      aSubjects=self.aPersons, aTimes=[sTime], 
                                      aScores=[self.nLikelihood], aObjects=[], location="");
        graph.addnode(self.rootAct, [], nPrior);
        graph.addnode(self.obsNode, [self.rootAct], [self.nLikelihood]);
        graph.addobs(self.obsNode, True);
        
    def getNodeName(self, sId, sActName, sTime, aSubjects):
        sSubjects = "(";
        for i in range(len(aSubjects)):
            if i==0: sSubjects += aSubjects[i];
            elif i>0: sSubjects += ", " + aSubjects[i];
        sSubjects += ")"
        return sId+"-"+sActName+"_"+sTime+"\\n"+sSubjects;
        
    def getKeyTexts(self):
        return [self.sActivity, self.aPersons];
            